package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private AttrKeyService attrKeyService;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page,queryVo);
    }

    @Override
    @Transactional
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long roomId = roomSubmitVo.getId();
        if(roomId != null){
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, roomId)
                    .eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        this.saveOrUpdate(roomSubmitVo);
        roomId = roomSubmitVo.getId();

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomId);
            graphInfoList.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfoList);

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
        for (Long attrValueId : attrValueIds) {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                    .roomId(roomId)
                    .attrValueId(attrValueId)
                    .build();
            roomAttrValueList.add(roomAttrValue);
        }
        roomAttrValueService.saveBatch(roomAttrValueList);

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
        for (Long facilityInfoId : facilityInfoIds) {
            RoomFacility roomFacility = RoomFacility.builder()
                    .roomId(roomId)
                    .facilityId(facilityInfoId)
                    .build();
            roomFacilityList.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilityList);

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = new ArrayList<>(labelInfoIds.size());
        for (Long labelInfoId : labelInfoIds) {
            RoomLabel roomLabel = RoomLabel.builder()
                    .labelId(labelInfoId)
                    .roomId(roomId)
                    .build();
            roomLabelList.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabelList);

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
        for (Long paymentTypeId : paymentTypeIds) {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                    .paymentTypeId(paymentTypeId)
                    .roomId(roomId)
                    .build();
            roomPaymentTypeList.add(roomPaymentType);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypeList);

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
        for (Long leaseTermId : leaseTermIds) {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                    .leaseTermId(leaseTermId)
                    .roomId(roomId)
                    .build();
            roomLeaseTermList.add(roomLeaseTerm);
        }
        roomLeaseTermService.saveBatch(roomLeaseTermList);
    }

    @Override
    public void removeRoomById(Long id) {
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        LeaseAgreement leaseAgreement = leaseAgreementService.getOne(lambdaQueryWrapper);
        if(leaseAgreement.getStatus() == LeaseStatus.SIGNED
                || leaseAgreement.getStatus() == LeaseStatus.WITHDRAWING
                ||leaseAgreement.getStatus() == LeaseStatus.RENEWING){
            throw new LeaseException(ResultCodeEnum.DELETE_ROOM_ERROR);
        }

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        this.removeById(id);
    }

    @Override
    @Transactional
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,ItemType.ROOM);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>(graphInfoList.size());
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo,graphVo);
            graphVoList.add(graphVo);
        }
        roomDetailVo.setGraphVoList(graphVoList);

        LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        List<RoomAttrValue> roomAttrValueList = roomAttrValueService.list(attrValueLambdaQueryWrapper);
        List<AttrValueVo> attrValueVoList = new ArrayList<>(roomAttrValueList.size());
        for (RoomAttrValue roomAttrValue : roomAttrValueList) {
            AttrValueVo attrValueVo = new AttrValueVo();
            AttrValue attrValue = attrValueService.getById(roomAttrValue.getAttrValueId());
            String name = attrKeyService.getById(attrValue.getAttrKeyId()).getName();
            attrValueVo.setAttrKeyName(name);
            BeanUtils.copyProperties(attrValue,attrValueVo);
            attrValueVoList.add(attrValueVo);
        }
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<RoomInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(RoomInfo::getId,id)
                .set(RoomInfo::getIsRelease,status);
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public List<RoomInfo> listBasicByApartmentId(Long id) {
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        return this.list(lambdaQueryWrapper);
    }
}




